var sscAi = function() {
    this.arr3 = ['一', '二', '三', '四', '五'];
    ai.call(this);
};
sscAi.prototype = Object.create(ai.prototype);

// 记录到受影响的数字
sscAi.prototype.insertToNumber = function(list) {
    // 记录到各受影响的号码
    var result = [];
    for (var i = 0; i < 5; i++) {
        result[i] = {
            index: i + 1,
            total: 0,
            bet: [],
        };
    }
    for (var i in list) {
        var k = list[i].key;
        if (k == '龙' || k == '虎' || k == '和') {
            result[0].bet.push(k);
            result[0].total += list[i].amount;
            result[4].bet.push(k);
            result[4].total += list[i].amount;
        }
        else if (k.startsWith("和")) {
            for (var j = 0; j < 5; j++) {
                result[j].bet.push(k);
            }
        }
        else if (k.startsWith("前") || k.startsWith("中") || k.startsWith("后")) {
            continue;
        }
        else {
            var index = this.arr3.indexOf(k[0]);
            result[index].bet.push(k);
            result[index].total += list[i].amount;
        }
    }
    result.sort(function(a, b) {
        return b.total - a.total;
    });
    return result;
}

// 检测某个投注项，并应用概率
sscAi.prototype.apply = function(key, key2, arr1, arr2, all) {
    var self = this;
    if (key != key2) return null;
    if (this.checkRatio(self.rate)) {
        all = self.prior(all, arr1);
    }
    else {
        all = self.prior(all, arr2);
    }
    return all;
}

// 应用龙虎的概率，需要将信息记录到另外个关联号码
// lh - 用户的投注  lh2 - 实际开奖结果
sscAi.prototype.longhu = function(result, index, lh, lh2) {
    var self = this;
    var index2 = index == 1 ? 5 : 1;
    for (var i in result) {
        if (result[i].index != index2) continue;
        var data = result[i];

        self.deleteItem(data.bet, lh);
        if (!data.pre) data.pre = [lh2];
        else data.pre.push(lh2);
    }
    if (lh2 == '龙' && index == 1 || lh2 == '虎' && index == 5) {
        return [4, 5, 6, 7, 8, 9]
    }
    else if (lh2 == '龙' && index == 5 || lh2 == '虎' && index == 1) {
        return [0, 1, 2, 3, 4, 5];
    }
    return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
}

// 取得龙虎和的开奖数据
sscAi.prototype.longhuNumbers = function(index, lh, ns) {
    var index2 = index == 1 ? 5 : 1;
    var n = ns[index2 - 1];

    var arr = [];
    if (lh == '龙' && index < index2 || lh == '虎' && index > index2) {
        // 需要大于n
        for (var i = n + 1; i < 10; i++) arr.push(i);
    }
    else if (lh == '不中') {
        // 不要相等就可以
        for (var i = 0; i < 10; i++) {
            if (i !== n) arr.push(i);
        }
    }
    else if (lh == '中') {
        arr.push(n);
    }
    else {
        // 需要小于n
        for (var i = 0; i < n; i++) arr.push(i);
    }
    return arr;
}

// 应用总和的概率
sscAi.prototype.he = function(result, index, he, he2) {
    var self = this;
    for (var i in result) {
        var data = result[i];
        self.deleteItem(data.bet, he);
    }
    if (!result[4].pre) result[4].pre = [he2];
    else result[4].pre.push(he2);

    if (he2 == '和大') {
        return [4, 5, 6, 7, 8, 9];
    }
    if (he2 == '和小') {
        return [0, 1, 2, 3, 4, 5];
    }
    return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
}

// 取得总和的开奖数据
sscAi.prototype.heNumbers = function(index, he, ns) {
    var total = 0;
    var arr = [];
    for (var i = 0; i < 5; i++) {
        if (i + 1 == index) continue;
        total += ns[i];
    }
    if (he == '和单' && total % 2 == 0 || he == '和双' && total % 2 == 1) {
        return [1, 3, 5, 7, 9];
    }
    if (he == '和单' && total % 2 == 1 || he == '和双' && total % 2 == 0) {
        return [0, 2, 4, 6, 8];
    }
    if (he == '和大') {
        for (var i = 0; i <= 9; i++) {
            if (total + i >= 23) arr.push(i);
        }
    }
    if (he == '和小') {
        for (var i = 0; i <= 9; i++) {
            if (total + i < 23) arr.push(i);
        }
    }
    return arr;
}

// 入口
sscAi.prototype.open = function(bets, lottery, rate) {
    var self = this;
    self.map = lottery;
    self.rate = rate;
    
    // 合并下数据
    var arr = [["和大", "和小"], ["和单", "和双"], ["龙", "虎"]];
    var arr2 = self.arr3;
    for (var i in arr2) {
        var k = arr2[i];
        arr.push([k + "大", k + "小"]);
        arr.push([k + "单", k + "双"]);
    }
    var list = self.mixBets(bets, arr);

    // 记录到各受影响的号码
    var result = self.insertToNumber(list);

    // 逐个号码开出来
    var numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    var lottery = [];
    var ns = [];
    for (var i in result) {
        var index = result[i].index;
        var bets = result[i].bet;
        if (self.isEmpty(bets) && self.isEmpty(result[i].pre)) continue;

        var all = numbers.concat([]);
        if (result[i].pre) {
            // 这些受其他号码影响，需要优先处理
            for (var j in result[i].pre) {
                var k = result[i].pre[j];
                if (k == '龙' || k == '虎' || k == '中' || k == '不中') {
                    all = self.prior(all, self.longhuNumbers(index, k, ns));
                }
                else if (k.startsWith("和")) {
                    all = self.prior(all, self.heNumbers(index, k, ns));
                }
            }
        }
        for (var j in bets) {
            var k = bets[j];
            if (!k) continue;
            var k2 = k.substring(1);
            do {
                if (k == '龙' || k == '虎') {
                    if (self.checkRatio(rate)) {
                        all = self.prior(all, self.longhu(result, index, k, k == '龙' ? '虎' : '龙'));
                    }
                    else {
                        all = self.prior(all, self.longhu(result, index, k, k));
                    }
                }
                else if (k == '和') {
                    if (!self.checkRatio(0.9) && !self.checkRatio(rate)) {
                        // 中
                        self.longhu(result, index, k, k);
                    }
                    else {
                        // 不中
                        self.longhu(result, index, k, '不中');
                    }
                }
                else if (k == '和大') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和小'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                }
                else if (k == '和小') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和大'));
                    else 
                    all = self.prior(all, self.he(result, index, k, k));
                }
                else if (k == '和单') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和双'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                }
                else if (k == '和双') {
                    if (self.checkRatio(rate)) 
                        all = self.prior(all, self.he(result, index, k, '和单'));
                    else 
                        all = self.prior(all, self.he(result, index, k, k));
                }

                var t = self.apply(k2, "大", [0, 1, 2, 3, 4], [5, 6, 7, 8, 9], all);
                if (t != null) { all = t; break; }
                var t = self.apply(k2, "小", [5, 6, 7, 8, 9], [0, 1, 2, 3, 4], all);
                if (t != null) { all = t; break; }
                var t = self.apply(k2, "单", [0, 2, 4, 6, 8], [1, 3, 5, 7, 9], all);
                if (t != null) { all = t; break; }
                var t = self.apply(k2, "双", [1, 3, 5, 7, 9], [0, 2, 4, 6, 8], all);
                if (t != null) { all = t; break; }

                if (true) {
                    // 正码
                    var n = parseInt(k2);
                    var p = 9 * rate / (9 * rate + 1 - rate);
                    if (self.checkRatio(p)) {
                        var temp = [];
                        for (var m = 0; m < 10; m++) if (m != n) temp.push(m);
                        all = self.prior(all, temp);
                    }
                    else {
                        all = self.prior(all, [n]);
                    }
                    break;
                }
            } while (false);
            if (all.length == 1) break;
        }

        // 应用概率，抽取一个出来
        ns[index - 1] = self.random(all);
    }

    // 还没有开出的号码，随机开了
    for (var i = 0; i < 5; i++) {
        if (ns[i] !== undefined) continue;
        ns[i] = self.random(numbers);
    }

    // 返回开奖结果
    return ns;
}
var SscAi = new sscAi();
